package com.hruiworks.campusgroup.pojo.query;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.fasterxml.jackson.annotation.JsonFormat;
import com.hruiworks.campusgroup.common.BasePageQuery;
import com.hruiworks.campusgroup.enums.ErrorCode;
import com.hruiworks.campusgroup.exception.BusinessException;
import com.hruiworks.campusgroup.pojo.entity.Group;
import lombok.Data;
import lombok.EqualsAndHashCode;
import org.apache.commons.lang3.StringUtils;

import java.io.Serializable;
import java.time.LocalDateTime;
import java.util.Objects;

/**
 * 活动VO
 *
 * @author jiushui
 */
@EqualsAndHashCode(callSuper = true)
@Data
public class GroupPageQuery extends BasePageQuery implements Serializable {

    /**
     * 自增主键
     */
    private Long id;

    /**
     * 描述
     */
    private String description;

    /**
     * 活动地点
     */
    private String address;

    /**
     * 需要人数
     */
    private Integer neededNum;

    /**
     * 团队状态，0表示待成团，1表示成团成功，2表示失败
     */
    private Integer groupStatus;

    /**
     * 联系方式
     */
    private String contactInfo;

    /**
     * 已有人数
     */
    private Integer alreadyNum;

    /**
     * 创建人id
     */
    private Long createId;

    /**
     * 所属活动id
     */
    private Long activityId;

    /**
     * 活动开始时间
     */
    @JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss")
    private LocalDateTime startTime;

    /**
     * 活动结束时间
     */
    @JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss")
    private LocalDateTime endTime;

    public LambdaQueryWrapper<Group> baseLambdaQuery() {
        Long id = this.getId();
        String description = this.getDescription();
        String address = this.getAddress();
        Integer neededNum = this.getNeededNum();
        Integer groupStatus = this.getGroupStatus();
        String contactInfo = this.getContactInfo();
        Integer alreadyNum = this.getAlreadyNum();
        Long createId = this.getCreateId();
        Long activityId = this.getActivityId();
        LocalDateTime startTime = this.getStartTime();
        LocalDateTime endTime = this.getEndTime();
        Long current = this.getCurrent();
        Long size = this.getSize();

        if (Objects.isNull(current) || Objects.isNull(size)) {
            throw new BusinessException(ErrorCode.PARAMS_ERROR, "缺少分页参数");
        }

        LambdaQueryWrapper<Group> queryWrapper = new LambdaQueryWrapper<>();

        if (Objects.nonNull(id)) {
            queryWrapper.eq(Group::getId, id);
        }
        if (StringUtils.isNotBlank(description)) {
            queryWrapper.eq(Group::getDescription, description);
        }
        if (StringUtils.isNotBlank(address)) {
            queryWrapper.eq(Group::getAddress, address);
        }
        // 干脆不允许通过截止时间进行查询算了……
        queryWrapper.gt(Group::getExpirationTime, LocalDateTime.now());

        if (Objects.nonNull(neededNum)) {
            queryWrapper.eq(Group::getNeededNum, neededNum);
        }
        if (Objects.nonNull(groupStatus)) {
            queryWrapper.eq(Group::getGroupStatus, groupStatus);
        }
        if (StringUtils.isNotBlank(contactInfo)) {
            queryWrapper.eq(Group::getContactInfo, contactInfo);
        }
        if (Objects.nonNull(alreadyNum)) {
            queryWrapper.eq(Group::getAlreadyNum, alreadyNum);
        }
        if (Objects.nonNull(createId)) {
            queryWrapper.eq(Group::getCreateId, createId);
        }
        if(Objects.nonNull(activityId)) {
            queryWrapper.eq(Group::getActivityId, activityId);
        }
        if (Objects.nonNull(startTime)) {
            queryWrapper.gt(Group::getStartTime, startTime);
        }
        if (Objects.nonNull(endTime)) {
            queryWrapper.lt(Group::getEndTime, endTime);
        }

        return queryWrapper;
    }

}
